package com.ktjy.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ktjy.entity.SysRight;
import com.ktjy.service.IBasDictService;
import com.ktjy.service.ISysRightService;
import com.ktjy.utils.TreeViewUtils;
import com.ktjy.utils.TreeViewVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * <p>
 * 权限资源表 控制器
 * </p>
 *
 * @author liuqiaomiao
 * @ClassName SysRightController
 * @since 2024-03-05
 */
@Controller
@RequestMapping("/right")
public class SysRightController {

    @Autowired
    private ISysRightService sysRightService;


    @Autowired
    private IBasDictService basDictService;

    /**
     * 跳转新增页面
     */
    @RequestMapping(value = "/add")

//    @RequiresPermissions("L060301")
    public Object toInsert(Model model) {

        model.addAttribute("rightTypes", basDictService.selectTypeList("菜单类型"));
        List<SysRight> list = sysRightService.list();
        List<TreeViewVo> resultList = TreeViewUtils.formatTreeView(list);
        model.addAttribute("parentList", JSON.toJSONString(resultList));

        return "/right/add";
    }

    /**
     * 增
     */
    @PostMapping(value = "/save")
//    @ResponseBody
    public Object insert(SysRight sysRight) {
        if (sysRightService.getById(sysRight.getRightCode()) != null) {
            sysRightService.updateById(sysRight);
        } else {
            sysRightService.save(sysRight);
        }
        return "redirect:list";
    }

    /**
     * 删
     */
    @RequestMapping(value = "/del/{id}")
    @ResponseBody
    public Object delete(@PathVariable(name = "id") Long id) {
        return sysRightService.removeById(id);
    }

    /**
     * 跳转修改页面
     */
//
    @RequestMapping(value = "/edit")
    public Object toUpdate(String id, Model model) {
        model.addAttribute("rightTypes", basDictService.selectTypeList("菜单类型"));
        List<SysRight> list = sysRightService.list();

//        List<TreeViewVo>resultList= TreeViewUtils.formatTreeView(list);
//        model.addAttribute("parentList", JSON.toJSONString(resultList));
        SysRight resutlRight = sysRightService.getById(id);
        model.addAttribute("right", resutlRight);
        SysRight right = list.stream().filter(sysRight -> sysRight.getRightCode().equals(resutlRight.getRightParentCode())).findAny().get();
        model.addAttribute("rightParentCodeText", right.getRightText());
        return "/right/edit";
    }


    /**
     * 分页列表
     */
    @RequestMapping(value = "/list")
    public Object getUserPage(Page page, SysRight sysRight, Model model) {
        page.setSize(10000);
//        Page pageEntity = sysRightService.page(page, new QueryWrapper<>(sysRight));

//        pageEntity.setRecords(TreeUtils.getChildList(pageEntity.getRecords(),"ROOT_MENU"));
//        System.out.println(JSON.toJSONString(pageEntity));
        model.addAttribute("rightPage", sysRightService.list());
        return "/right/list";
    }

}
